# -*- encoding: utf-8 -*-

import random


class SNAKE:
    snakeBody = []

    def __init__(self, screen: tuple, size: int, sanke_length: int, body_position: list):
        self.sanke_length = sanke_length
        self.body_position = body_position
        self.screen = screen
        self.size = size
        self.direction = ["up", "down", "left", "right"]
        self.createdBody()
# 获取随机坐标

    def getRandom(self, type: str):
        return int(random.randrange(0, (self.screen[0] if type == "x" else self.screen[1])//self.size)*self.size)

# 判断列表里是否存在指定列表
    def checkList(self, list1: list, list2: list):
        return False if list1 in list2 else True

# 创建蛇的身体

    def createdBody(self):
        if not self.body_position:
            new_position = [self.getRandom("x"), self.getRandom("y")]
            self.snakeBody.insert(0, new_position)
            for length in range(1, self.sanke_length):
                while True:
                    direction = self.direction[random.randrange(0, 3)]
                    if direction == "up":
                        new_position = [new_position[0],
                                        new_position[1] - self.size]
                        if new_position[1] >= 0 and self.checkList(new_position, self.snakeBody):
                            self.snakeBody.insert(0, new_position)
                            break
                    elif direction == "down":
                        new_position = [new_position[0],
                                        new_position[1]+self.size]
                        if new_position[1] <= self.screen[1] and self.checkList(new_position, self.snakeBody):
                            self.snakeBody.insert(0, new_position)
                            break
                    elif direction == "left":
                        new_position = [new_position[0] -
                                        self.size, new_position[1]]
                        if new_position[0] >= 0 and self.checkList(new_position, self.snakeBody):
                            self.snakeBody.insert(0, new_position)
                            break
                    elif direction == "right":
                        new_position = [new_position[0] +
                                        self.size, new_position[1]]
                        if new_position[0] <= self.screen[0] and self.checkList(new_position, self.snakeBody):
                            self.snakeBody.insert(0, new_position)
                            break
            return 1
        elif self.sanke_length == len(self.body_position):
            self.snakeBody = self.body_position
            return 1
        else:
            return 0
# 移动蛇身

    def moveBody(self, DIR: int):
        direction = self.direction[DIR]
        if direction == "up":
            self.snakeBody.insert(
                0, [self.snakeBody[0][0], self.snakeBody[0][1]-self.size])
        elif direction == "down":
            self.snakeBody.insert(
                0, [self.snakeBody[0][0], self.snakeBody[0][1]+self.size])
        elif direction == "left":
            self.snakeBody.insert(
                0, [self.snakeBody[0][0]-self.size, self.snakeBody[0][1]])
        elif direction == "right":
            self.snakeBody.insert(
                0, [self.snakeBody[0][0]+self.size, self.snakeBody[0][1]])
        self.snakeBody.pop()

# 返回蛇的身体

    def getBody(self):
        return self.snakeBody
# 获取蛇的长度

    def getLength(self):
        return self.sanke_length

# 设置头部
    def headSetting(self, position_x, position_y):
        if position_x == -1:
            position_x = self.snakeBody[0][0]
        if position_y == -1:
            position_y = self.snakeBody[0][1]
        self.snakeBody[0] = [position_x, position_y]
